
package com.aizhixin.lab.project.course.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.company.domain.UserDomain;
import com.aizhixin.lab.project.course.core.TaskStatus;
import com.aizhixin.lab.project.course.domain.*;
import com.aizhixin.lab.project.course.service.ProjectGroupTaskService;
import com.aizhixin.lab.project.course.vo.StuTaskNumVo;
import com.aizhixin.lab.project.course.vo.StuTaskScheduleVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/v1/teacher/project/task")
@RestController
@Api(tags = "发布任务",value = "发布任务管理")
public class ProjectGroupTaskController {
    @Autowired
    private ProjectGroupTaskService taskService;
    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "/releaseTask", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "发布任务",  notes = "发布任务<br>@author lwq")
    public ResponseEntity<?> releaseTask(@RequestHeader("Authorization") String token,
                                         @ApiParam(value = "ReleaseTaskDomain", required = false) @RequestBody ReleaseTaskDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        result= taskService.releaseTask(domain);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/stuReleaseTask", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "学生发布任务",  notes = "学生发布任务<br>@author lwq")
    public ResponseEntity<?> stuReleaseTask(@RequestHeader("Authorization") String token,
                                         @ApiParam(value = "ReleaseTaskDomain", required = false) @RequestBody StuReleaseTaskDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        String s = taskService.stuReleaseTask(domain);
        result.put(ApiReturnConstants.SUCCESS,true);
        result.put(ApiReturnConstants.DATA,s);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findStuTaskList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询学生项目任务列表",  notes = "查询学生项目任务列表<br>@author lwq")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = StuTaskListDomain.class)})
    public ResponseEntity<?> findStuTaskList(@RequestHeader("Authorization") String token,
                                                 @ApiParam(value = "项目ID", required = false) @RequestParam String projectId,
                                             @ApiParam(value = "学生ID", required = false) @RequestParam Long stuId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        result= taskService.findMyTaskList(projectId, stuId,null);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findStuTaskGrade", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询学生项目任务成绩列表",  notes = "查询学生项目任务成绩列表<br>@author lwq")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = StuTaskListDomain.class),})
    public ResponseEntity<?> findStuTaskGrade(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "项目ID", required = false) @RequestParam String projectId,
                                             @ApiParam(value = "学生ID", required = false) @RequestParam Long stuId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        result= taskService.findMyTaskList(projectId, stuId, TaskStatus.TASK_STATUS_APPROVED);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findStuTaskInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询学生任务详情",  notes = "查询学生任务详情<br>@author lwq")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = StuTaskInfoDomain.class),})
    public ResponseEntity<?> findStuTaskInfo(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "学生任务ID", required = false) @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        StuTaskInfoDomain stuTaskInfo = taskService.findStuTaskInfo(id,account.getId());
        result.put(ApiReturnConstants.DATA, stuTaskInfo);
        result.put(ApiReturnConstants.SUCCESS, true);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/approvalStuTask", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "批阅学生任务",  notes = "批阅学生任务<br>@author lwq")
    public ResponseEntity<?> approvalStuTask(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "学生任务ID", required = false) @RequestBody StuTaskDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        if (StringUtils.isEmpty(domain.getId())){
            result.put(ApiReturnConstants.SUCCESS,false);
            result.put(ApiReturnConstants.MESSAGE,"任务ID为空");
            return new ResponseEntity(result, HttpStatus.OK);
        }

        result = taskService.approvalStuTask(domain, account);

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getTaskDegreeOfCompletionDomain", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查看某个学生的任务完成数",  notes = "获取任务完成情况<br>@author lwq")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = TaskDegreeOfCompletionDomain.class),})
    public ResponseEntity<?> getTaskDegreeOfCompletionDomain(@RequestHeader("Authorization") String token,
                                                             @ApiParam(value = "项目ID", required = false) @RequestParam String projectId,
                                                             @ApiParam(value = "学生ID", required = false) @RequestParam Long stuId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        TaskDegreeOfCompletionDomain taskDegreeOfCompletionDomain = taskService.getTaskDegreeOfCompletionDomain(projectId, stuId);
        result.put(ApiReturnConstants.DATA, taskDegreeOfCompletionDomain);
        result.put(ApiReturnConstants.SUCCESS, true);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findStuTaskNum", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "获取任务完成数",  notes = "获取任务完成数<br>@author lwq")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = TaskDegreeOfCompletionDomain.class),})
    public ResponseEntity<?> findStuTaskNum(@RequestHeader("Authorization") String token,
                                                             @ApiParam(value = "项目ID", required = false) @RequestParam String projectId,
                                                             @ApiParam(value = "学生ID", required = false) @RequestParam Long stuId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        StuTaskNumVo stuTaskNum = taskService.findStuTaskNum(projectId, stuId);
        result.put(ApiReturnConstants.DATA, stuTaskNum);
        result.put(ApiReturnConstants.SUCCESS, true);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/submitTask", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "提交任务工单",  notes = "提交任务工单")
    public ResponseEntity<?> submitTask(@RequestHeader("Authorization") String token,
                                         @ApiParam(value = "ReleaseTaskDomain", required = false) @RequestBody SubmitTaskDomain domain
    ) {
//        AccountDTO account = accountService.getUserByToken(token);
//        if (account == null) {
//            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//        }
        Map<String, Object> result =  new HashMap<String, Object>();
//        HttpRequest get = HttpUtil.createGet("https://s1.aizhixin.com/1611544301624.zip");
//        String get1 = HttpUtil.get("https://s1.aizhixin.com/1611544301624.zip");
//        File file = new File("D:\\360Safe\\aa");
//        HttpUtil.downloadFile("https://s1.aizhixin.com/1611544301624.zip",file);
        taskService.submitTask(domain);

//        File file1 = new File("D:\\360Safe\\aa.zip");
//        BeanUtils.copyProperties(file,file1);
//        result.put(ApiReturnConstants.DATA,get);
//        result.put(ApiReturnConstants.DATA,get1);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/TeacherCheckTask", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "审核任务工单",  notes = "审核任务工单")
    public ResponseEntity<?> TeacherCheckTask(@RequestHeader("Authorization") String token,
                                        @ApiParam(value = "ReleaseTaskDomain", required = false) @RequestBody TeacherCheckTaskDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        taskService.teacherCheckTask(domain);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/taskSchedule", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "任务进度",  notes = "任务进度")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = StuTaskScheduleVo.class)})
    public ResponseEntity<?> taskSchedule(@RequestHeader("Authorization") String token,
                                              @ApiParam(value = "ReleaseTaskDomain", required = false) @RequestBody TaskScheduleDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        StuTaskScheduleVo scheduleVo = taskService.taskSchedule(domain);
        result.put(ApiReturnConstants.DATA, scheduleVo);
        result.put(ApiReturnConstants.SUCCESS, true);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/scheduleManagement", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "进度管理",  notes = "进度管理")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = StuTaskScheduleVo.class)})
    public ResponseEntity<?> scheduleManagement(@RequestHeader("Authorization") String token,
                                          @ApiParam(value = "ReleaseTaskDomain", required = false) @RequestBody TaskSchedulePageDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        Map<String, Object> scheduleVo = taskService.scheduleManagement(domain);
        result.put(ApiReturnConstants.DATA, scheduleVo);
        result.put(ApiReturnConstants.SUCCESS, true);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findProjectGanttChart", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = TaskGanttDiagramDomain.class)})
    @ApiOperation(value = "项目任务进度-甘特图", httpMethod = "GET", notes = "项目任务进度-甘特图")
    public ResponseEntity<?> findProjectGanttChart(@RequestHeader("Authorization") String token,
                                                   @ApiParam(value = "项目ID", required = false) @RequestParam String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            List<TaskGanttDiagramDomain> publish = taskService.findProjectGanttChart(projectId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,publish);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return  new ResponseEntity(result, HttpStatus.OK);
    }
}
